﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class city : System.Web.UI.Page
{

    string my_substring(string text)
    {
        int l = 60;
        if(DataUtil.IsEmptyString(text))
            return text;
        if (text.Length < l)
            return text;
        else
        {

            return text.Substring(0, l) + "...";
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        //init helper class
            GeoLingManager glManager = new GeoLingManager(Request, Response);
            String _lang = glManager.getLang();
            DatabaseUtil dbUtil = new DatabaseUtil();
            //Get user's preferred langugage in int
            _lang = dbUtil.getLanguageNo(_lang).ToString();
        int no = int.Parse(Request["no"]);
        ctpDataContext db=new ctpDataContext();

        ct_profile ct = db.ct_profiles.Single(p => p.no == no);
        String title = dbUtil.getPropertyPost(ct.title, (int)DatabaseUtil.fromTables.ct_profile_title, ct.no, _lang);
        //this.Label6.Text=title;

        this.Label12.Text ="Top users' "+ title;
        this.Label11.Text = title + " Today";
        this.HyperLink20.NavigateUrl = "create_city_s.aspx?no=" + no;
        if (!this.IsPostBack)
        {

            var cts = from p in db.ct_s_profiles
                      where p.valid == true && p.ct_no == no
                      select p;

            ct.ct_s_amount = cts.Count();
            db.SubmitChanges();


            var list1 = from p in db.ct_s_profiles
                        where p.ct_no == no && p.type_no != 99 && p.valid == true
                        orderby p.add_time descending
                        select p.no;
            
            Hashtable postsTable = dbUtil.getTranlatePosts((int)DatabaseUtil.fromTables.s_profile_title, list1.Distinct().ToArray());
            Hashtable langTable = dbUtil.getLanguageTable(_lang);
            var data1 = from p in db.ct_s_profiles
                        where p.ct_no == no&&p.type_no!=99&&p.valid==true
                        orderby p.add_time descending
                        select new
                        {
                            title = dbUtil.getPropertyPost(p.title, postsTable, p.no,_lang),
                            //p.add_time,
                            p.user.nickname,
                            p.create_user_no,
                            p.intro,
                            main_pic_fn="city_imgs/1/"+p.main_pic_fn.Trim(),
                            profile_picture="user_imgs/1/"+p.user.profile_picture.Trim(),
                            tourl="city_s.aspx?no="+p.no.ToString(),
                            no = p.no,
                            original_text = dbUtil.getOriginalPost(p.title, postsTable, p.no),
                            original_label = dbUtil.getOriginalLang(postsTable, langTable, p.no,_lang)
                        };
            Hashtable data1no = new Hashtable();
            foreach (var d in data1.Take(10))
            {
                data1no.Add(d.no, null);
            }
            var list2 = from p in db.ct_s_profiles
                        join q in db.ct_s_profiles
                        on p.parent equals q.no
                        where p.ct_no == no && p.type_no == 99 && p.valid == true && q.valid == true
                        orderby p.add_time descending
                        select p.no;
            var list2_1 = from p in db.ct_s_profiles
                          join q in db.ct_s_profiles
                              on p.parent equals q.no
                          where p.ct_no == no && p.type_no == 99 && p.valid == true && q.valid == true
                          orderby p.add_time descending
                          select q.no;
            Hashtable titlesTable = dbUtil.getTranlatePosts((int)DatabaseUtil.fromTables.s_profile_title, list2.Distinct().ToArray());
            Hashtable parentTitlesTable = dbUtil.getTranlatePosts((int)DatabaseUtil.fromTables.s_profile_title, list2_1.Distinct().ToArray());
            Hashtable introsSTable = dbUtil.getTranlatePosts((int)DatabaseUtil.fromTables.s_profile_intro, list2.Distinct().ToArray());
            var data2 = from p in db.ct_s_profiles join q in db.ct_s_profiles
                                                   on p.parent equals q.no
                        where p.ct_no == no && p.type_no == 99 &&p.valid==true && q.valid==true
                        orderby p.add_time descending
                        select new
                        {

                            title = dbUtil.getPropertyPost(p.title, titlesTable, p.no, _lang),
                            //p.add_time,
                            p.user.nickname,
                            p.create_user_no,
                            intro = dbUtil.getPropertyPost(p.intro, introsSTable, p.no, _lang),
                            main_pic_fn = "city_imgs/1/" + p.main_pic_fn.Trim(),
                            parent_title = dbUtil.getPropertyPost(q.title, parentTitlesTable, q.no, _lang),
                            p.parent,

                            profile_picture="user_imgs/1/"+p.user.profile_picture.Trim(),
                            tourl="city_s.aspx?no="+p.parent.ToString()

                        };


            var list3 = from p in db.ct_s_intros
                        join q in db.ct_s_profiles on p.ct_s_no equals q.no
                        where p.ct_no == no && q.valid == true
                        orderby p.addtime descending
                        select p.no;
            var list3_1 = from p in db.ct_s_intros
                          join q in db.ct_s_profiles on p.ct_s_no equals q.no
                          where p.ct_no == no && q.valid == true
                          orderby p.addtime descending
                          select  q.no ;
            titlesTable = dbUtil.getTranlatePosts((int)DatabaseUtil.fromTables.s_profile_title, list3_1.Distinct().ToArray());
            Hashtable introsTable = dbUtil.getTranlatePosts((int)DatabaseUtil.fromTables.intro_intro, list3.Distinct().ToArray());
            var data3 = from p in db.ct_s_intros join q in db.ct_s_profiles on p.ct_s_no equals q.no
                        where p.ct_no == no&&q.valid==true
                        orderby p.addtime descending
                        select new
                        {
                            p.create_user_no,
                            intro = this.my_substring( dbUtil.getPropertyPost(p.intro, introsTable, p.no, _lang)),

                            main_pic_fn = "city_imgs/1/" + q.main_pic_fn.Trim(),

                            title = dbUtil.getPropertyPost(q.title.Trim(), titlesTable, q.no, _lang),
                            nickname=p.user.nickname.Trim(),
                            tourl="city_s.aspx?no="+p.ct_s_no.ToString(),
                            original_text = dbUtil.getOriginalPost(q.title, titlesTable, q.no),
                            original_label = dbUtil.getOriginalLang(titlesTable, langTable, q.no, _lang),
                            title_no = q.no+"_"+p.create_user_no,
                            intro_no = p.no+"_"+p.create_user_no,
                            original_text_intro = this.my_substring(dbUtil.getOriginalPost(p.intro, introsTable, p.no)),
                            original_label_intro = dbUtil.getOriginalLang(introsTable, langTable, p.no, _lang)
                        };
            Hashtable data3title = new Hashtable();
            Hashtable data3intro = new Hashtable();
            foreach (var d in data3.Take(10))
            {
                string key = d.title_no;
                if(!data3title.ContainsKey(key))
                 data3title.Add(key, null);
                key = d.intro_no;
                if (!data3intro.ContainsKey(key))
                    data3intro.Add(key, null);
            }
     

            //foreach (var i in data3)
            //{
            //    if(i.intro.Length>20)
            //    {
            //        i.intro = i.intro.Substring(0, 20);
               
            //    }
            //}

            var data4 = from p in db.my_citiport_mines
                        join q in db.users on p.userno equals q.no
                        where p.ct_no == no
                        orderby p.recommend descending
                        select new
                        {
                            tourl="my/default.aspx?no="+p.userno.ToString()+"&cno="+p.ct_no.ToString(),
                            profile_picture = "user_imgs/1/" + q.profile_picture.Trim(),
                            nickname=q.nickname.Trim()+" ("+p.recommend.ToString()+")",
                            no = p.ct_no

                        };


            this.GridView2.DataSource = data2.Take(10);

            this.GridView3.DataSource = data3.Take(10);

            this.DataList2.DataSource = data4;

            this.DataList1.DataSource = data1.Take(10);
            this.DataBind();
            Hashtable[] datanos = { data1no, data3title, data3intro};
            Control[] cs = { DataList1, GridView3, GridView3 };
            String[] ext = { "", "title", "intro" };
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "ScriptForDataList1", createYUIScript(datanos, cs, ext));



            
        }

    }

    private String createYUIScript(Hashtable[] contents, Control[] cs, String[] ext)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<script>");
        sb.Append("YAHOO.namespace(\"example.container\");");
        sb.Append("function init() {");
        for (int i = 0; i < cs.Length; i++)
        {
            sb.Append(UiUtil.BuildSimpleYUIPanels(contents[i],cs[i], ext[i]));     
        }
        sb.Append("}");
        sb.Append("YAHOO.util.Event.addListener(window, \"load\", init);");
        sb.Append("</script>");
        return sb.ToString();
    }
}
